/**
 * @ wenbin
 * @ date 2020/4/13
 */

/***
 *
 */
public class Solution2 {
    public String replaceSpace(StringBuffer str) {
        String result = str.toString();
        if(result.contains(" ")){
            while(str.indexOf(" ") != str.lastIndexOf(" ")){
                int index = str.indexOf(" ");
                str.replace(index,index+1,"%20");
            }
            int index = str.indexOf(" ");
            str.replace(index,index+1,"%20");
            result = str.toString();
            return result;

        }else{
            return result;
        }
    }

        public String replaceSpace2(StringBuffer str) {
            int index = str.indexOf(" ");
            while(index != -1){
                str.replace(index,index+1,"%20");
                index = str.indexOf(" ",index);
            }
            String result = str.toString();
            return result;

        }

    public String replaceSpace3(StringBuffer str) {
        int P1 = str.length() - 1;
        for (int i = 0; i <= P1; i++)
            if (str.charAt(i) == ' ')
                str.append("  ");

        int P2 = str.length() - 1;
        while (P1 >= 0 && P2 > P1) {
            char c = str.charAt(P1--);
            if (c == ' ') {
                str.setCharAt(P2--, '0');
                str.setCharAt(P2--, '2');
                str.setCharAt(P2--, '%');
            } else {
                str.setCharAt(P2--, c);
            }
        }
        return str.toString();
    }

    public static void main(String[] args) {
        String data="We Are Happy ";
        Solution2 solution2 =new Solution2();
        String s =solution2.replaceSpace2(new StringBuffer(data));
        System.out.println(s);
    }
}
